package cn.lycode.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.lycode.common.core.controller.BaseController;
import cn.lycode.common.core.domain.AjaxResult;
import cn.lycode.common.core.domain.entity.SysRole;
import cn.lycode.common.core.domain.model.ConditionDTO;
import cn.lycode.common.core.domain.model.SelectIdsDTO;
import cn.lycode.common.core.page.PageResult;
import cn.lycode.system.domain.dto.ChangeStatusDTO;
import cn.lycode.system.domain.dto.sysrole.SysRoleAddDTO;
import cn.lycode.system.domain.dto.sysrole.SysRoleMenuDTO;
import cn.lycode.system.domain.dto.sysrole.SysRoleUpDTO;
import cn.lycode.system.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/sys-role")
@RequiredArgsConstructor
@Slf4j
public class SysRoleController extends BaseController {
    private final SysRoleService roleService;



    /**
     *  分页查询页面数据
     * @param conditionDTO 条件
     */
    @SaCheckPermission("system:role:list")
    @PostMapping("/list")
    public AjaxResult<PageResult<SysRole>> list(@RequestBody ConditionDTO conditionDTO) {
        return AjaxResult.success(roleService.list(conditionDTO));
    }

    /**
     * 新增角色
     */
    @SaCheckPermission("system:role:add")
    @PostMapping("/add")
    public AjaxResult<Object> add(@Validated @RequestBody SysRoleAddDTO dto) {
        if (roleService.hasExistsRoleName(dto.getRoleName(), null)) {
            return AjaxResult.failed("新增角色'" + dto.getRoleName() + "'失败，角色名称已存在");
        }
        return toAjax(roleService.insertRole(dto));
    }

    /**
     * 禁用\启用角色
     */
    @SaCheckPermission("system:role:disable")
    @PostMapping("/changeStatus")
    public AjaxResult<Object> changeStatus(@RequestBody ChangeStatusDTO dto) {
        roleService.checkRoleAllowed(dto.getRoleId()); //超级管理员不允许被禁用
        return toAjax(roleService.changeRoleStatus(dto));
    }

    /**
     * 更新角色
     */
    @SaCheckPermission("system:role:update")
    @PostMapping("/update")
    public AjaxResult<Object> update(@Validated @RequestBody SysRoleUpDTO dto) {
        roleService.checkRoleAllowed(dto.getId()); //超级管理员不允许被修改
        return toAjax(roleService.update(dto));
    }


    /**
     * 获取角色菜单权限
     */
    @SaCheckPermission("system:role:menu")
    @GetMapping("/menu")
    public AjaxResult<Object> getRoleMenu(Long roleId) {
        return AjaxResult.success(roleService.queryRoleMenu(roleId));

    }

    /**
     * 设置角色菜单权限
     */
    @SaCheckPermission("system:role:menu")
    @PostMapping("/setRoleMenus")
    public AjaxResult<Object> setRoleMenus(@RequestBody SysRoleMenuDTO dto) {
        roleService.setRoleMenus(dto);
        return AjaxResult.success();

    }




    /**
     * 修改角色菜单权限
     */

    /**
     * 删除角色
     */
    @SaCheckPermission("system:role:delete")
    @PostMapping("/delete")
    public AjaxResult<Object> delete(@RequestBody SelectIdsDTO dto) {
        return toAjax(roleService.remove(dto));
    }



//    /**
//     * 根据角色编号获取详细信息
//     */
//    @SaCheckPermission("system:role:query")
//    @GetMapping(value = "/{roleId}")
//    public AjaxResult<SysRole> getInfo(@PathVariable Long roleId) {
//        roleService.checkRoleDataScope(roleId);
//        return AjaxResult.success(roleService.selectRoleById(roleId));
//    }
//

//

//
//    /**
//     * 修改保存数据权限
//     */
//    @SaCheckPermission("system:role:edit")
//    @PutMapping("/dataScope")
//    public AjaxResult<Object> dataScope(@RequestBody SysRole role) {
//        //禁止操作超级管理员
//        roleService.checkRoleAllowed(role);
//        roleService.checkRoleDataScope(role.getRoleId());
//        return toAjax(roleService.authDataScope(role));
//    }
//
//

//
//    /**
//     * 获取角色选择框列表
//     */
//    @SaCheckPermission("system:role:query")
//    @GetMapping("/optionselect")
//    public AjaxResult<Object> optionselect() {
//        return AjaxResult.success(roleService.selectRoleAll());
//    }
//
////    /**
////     * 查询已分配用户角色列表
////     */
////    @SaCheckPermission("system:role:list")
////    @GetMapping("/authUser/allocatedList")
////    public TableDataInfo allocatedList(SysUser user) {
////        startPage();
////        List<SysUser> list = userService.selectAllocatedList(user);
////        return getDataTable(list);
////    }
////
////    /**
////     * 查询未分配用户角色列表
////     */
////    @SaCheckPermission("system:role:list")
////    @GetMapping("/authUser/unallocatedList")
////    public TableDataInfo unallocatedList(SysUser user) {
////        startPage();
////        List<SysUser> list = userService.selectUnallocatedList(user);
////        return getDataTable(list);
////    }
//
//    /**
//     * 取消授权用户
//     */
//    @SaCheckPermission("system:role:edit")
//    @PutMapping("/authUser/cancel")
//    public AjaxResult<Object> cancelAuthUser(@RequestBody SysUserRole userRole) {
//        return toAjax(roleService.deleteAuthUser(userRole));
//    }
//
//    /**
//     * 批量取消授权用户
//     */
//    @SaCheckPermission("system:role:edit")
//    @PutMapping("/authUser/cancelAll")
//    public AjaxResult<Object> cancelAuthUserAll(Long roleId, Long[] userIds) {
//        return toAjax(roleService.deleteAuthUsers(roleId, userIds));
//    }
//
//    /**
//     * 批量选择用户授权
//     */
//    @SaCheckPermission("system:role:edit")
//    @PutMapping("/authUser/selectAll")
//    public AjaxResult<Object> selectAuthUserAll(Long roleId, Long[] userIds) {
//        roleService.checkRoleDataScope(roleId);
//        return toAjax(roleService.insertAuthUsers(roleId, userIds));
//    }
//
//    /**
//     * 获取对应角色部门树列表
//     */
//    @SaCheckPermission("system:role:query")
//    @GetMapping(value = "/deptTree/{roleId}")
//    public AjaxResult<Object> deptTree(@PathVariable("roleId") Long roleId) {
////        ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
////        ajax.put("depts", deptService.selectDeptTreeList(new SysDept()));
//        return AjaxResult.success();
//    }
}
